package com.write;

import cn.hutool.core.collection.CollectionUtil;
import com.domain.WriteData;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.PdfPTable;
import com.pdf.utils.PDFUtils;

import java.util.List;
import java.util.Map;

/**
 * 写入表格数据类
 *
 */
public class TableWriteData implements WriteDataHandle {

    @Override
    public void write(Document document, WriteData writeData, Map<String, PdfPTable> pdf_table_map, String previousGroup) {
        try {
            Font font = writeData.getFont().getFont(writeData.getTextLevEnum());
            PdfPTable titleTable = PDFUtils.createTitleTable(Integer.valueOf(writeData.getRow()), font, writeData.getHead());
            this.write(writeData, titleTable);
            document.add(titleTable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void write(WriteData writeData, PdfPTable titleTable) {
        if (CollectionUtil.isEmpty(writeData.getListValue())) {
            for (int i = 0; i < 1; i++) {
                for (String data : writeData.getHead()) {
                    PDFUtils.addCell(titleTable, "", writeData.getFont().getFont(writeData.getTextLevEnum()));
                }
            }
            return;
        }
        for (List<WriteData> write_Data : writeData.getListValue()) {
            for (WriteData data : write_Data) {
                PDFUtils.addCell(titleTable, data.getStringValue(), data.getFont().getFont(data.getTextLevEnum()));
            }
        }
    }
}
